package com.whty.mongodemo.util;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class DateUtil {

  public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

  public static String YYYY_MM_DD_HH_MM_SS_MS = "yyyy-MM-dd HH:mm:ss.SSS";

  public static String getUTCTime() {
    LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    return dtf.format(now);
  }

  /**
   * 例子 2022-11-24T16:23:02.018+08:00 -> 2022-11-24 16:23:02.018
   *
   * @param time
   * @return
   */
  public static String formatOffsetDateTime(String time) {
    return DateUtil.formatOffsetDateTime(time, YYYY_MM_DD_HH_MM_SS);
  }

  public static String formatOffsetDateTime(String time, String pattern) {
    LocalDateTime date = LocalDateTime.parse(time, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    return date.format(DateTimeFormatter.ofPattern(pattern));
  }
}
